using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data;

namespace GhostLogger
{
    public class DebugInfo
    {

        #region Local Variables

        public static ArrayList debugPackages = new ArrayList();
        public static ArrayList tempDebugPackages = new ArrayList();
        public static ArrayList removeDebugPackages = new ArrayList();

        public static DataSet allPackages;
        public static DataSet dsFunctionList; 

        #endregion


        #region Compile

        /// <summary>
        /// Compile Packages
        /// </summary>
        public static void CompilePackages()
        {
            foreach (object package in tempDebugPackages)
            {
                DBConnection.Compile(package.ToString());
                debugPackages.Add(package);
            }

            //Check for invalid
            foreach (object package in tempDebugPackages)
            {
                int errorCount;
                if (DBConnection.GetPackageInvalid(package.ToString()))
                {
                    errorCount = DBConnection.GetPackagePragmaErrorCount(package.ToString());
                    if (errorCount > 0)
                    {
                        //Recompile
                        DBConnection.RecompilePragmaErrors(package.ToString());
                    }
                    else
                    {
                        //Unidenitified error
                        MessageBox.Show("Unidenitified error. Please check the package " + package.ToString() + " manually.", "Unidenitified error0", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }

            }
        } 

        #endregion

        #region Get Details


        /// <summary>
        /// Get parameter details of the selected method call
        /// </summary>
        /// <param name="selectedRow"></param>
        /// <returns></returns>
        public static ArrayList GetParamDetails(DataGridViewRow selectedRow)
        {
            string strPackageName = selectedRow.Cells["PACKAGE_NAME"].Value.ToString();
            string strMethodName = selectedRow.Cells["METHOD_NAME"].Value.ToString();

            ArrayList userArgs = DBConnection.RetrieveArgumentInfo(strPackageName, strMethodName);
            //StringBuilder sbParamDetails = new StringBuilder();


            //int counter = 0;  
            //if (userArgs.Count == 0)
            //{
            //    IEnumerator enumUserArgs = userArgs.GetEnumerator();
            //    while (enumUserArgs.MoveNext())
            //    {
            //        counter++;
            //        string strTemp = "S" + counter;
            //        sbParamDetails.Append(enumUserArgs.Current);
            //        sbParamDetails.Append("-");
            //        sbParamDetails.AppendLine(selectedRow.Cells[strTemp].Value.ToString());
            //    }   
            //}

            return userArgs;
        }

        
        #endregion

        #region Recompile


        /// <summary>
        /// Recompile packages.
        /// </summary>
        public static void RecompilePackages()
        {
            foreach (object package in removeDebugPackages)
            {
                DBConnection.Recompile(package.ToString());
                debugPackages.Remove(package.ToString());
            }
            removeDebugPackages.Clear();

        }

        
        #endregion
    }
}
